/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.h
  * @brief          : Header for main.c file.
  *                   This file contains the common defines of the application.
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H

#ifdef __cplusplus
extern "C" {
#endif

/* Includes ------------------------------------------------------------------*/
#include "stm32c0xx_hal.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "calibration.h"
/* USER CODE END Includes */

/* Exported types ------------------------------------------------------------*/
/* USER CODE BEGIN ET */
typedef union {
  #define ADC_CHANNEL_NUM 2
  uint16_t data[ADC_CHANNEL_NUM];
  struct{
    uint16_t rf1_voltage;
    uint16_t rf2_voltage;
  } fields;
} adc_raw_t;

typedef struct
{
    linear_param_t param;
    float bias[ADC_CHANNEL_NUM]; // 每个通道的补偿值，单位dB
} detector_config_t;

typedef struct
{
    uint8_t init;
    uint8_t address;
    detector_config_t config;
    /*待定*/
} __ALIGNED(8) detector_flash_t;

typedef struct
{
    struct
    {
      uint8_t frame;
      uint8_t address;
    } header;

    uint8_t bias_enable;
    float voltage[ADC_CHANNEL_NUM];
    float dBm[ADC_CHANNEL_NUM];
    float power[ADC_CHANNEL_NUM];

    detector_config_t config;

    union {
    uint8_t byte[4];
    uint16_t half[2];
    uint32_t word;
  } check;
} rf_detector_t;
/* USER CODE END ET */

/* Exported constants --------------------------------------------------------*/
/* USER CODE BEGIN EC */

/* USER CODE END EC */

/* Exported macro ------------------------------------------------------------*/
/* USER CODE BEGIN EM */

/* USER CODE END EM */

/* Exported functions prototypes ---------------------------------------------*/
void Error_Handler(void);

/* USER CODE BEGIN EFP */

/* USER CODE END EFP */

/* Private defines -----------------------------------------------------------*/
#define LED_Pin GPIO_PIN_0
#define LED_GPIO_Port GPIOA
#define RF1_VOLTAGE_Pin GPIO_PIN_1
#define RF1_VOLTAGE_GPIO_Port GPIOA
#define RF2_VOLTAGE_Pin GPIO_PIN_2
#define RF2_VOLTAGE_GPIO_Port GPIOA

/* USER CODE BEGIN Private defines */
#define LED_ON() HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET)
#define LED_OFF() HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_SET)
#define LED_TOGGLE() HAL_GPIO_TogglePin(LED_GPIO_Port, LED_Pin)

#define RF_DETECTOR_FRAME_HEADER 0xAA
#define DEFAULT_INITIAL_VALUE 0x01
#define DEFAULT_ADDRESS 0x01
#define DEFAULT_COMPENSATION 30.0f
#define DEFAULT_VREF_VOLTAGE 3.3f
#define DEFAULT_ADC_RESOLUTION 0x0fff

/* USER CODE END Private defines */

#ifdef __cplusplus
}
#endif

#endif /* __MAIN_H */
